package cn.fay.chapter16;

import java.lang.reflect.Array;
import java.util.Arrays;

import cn.fay.chapter15.Generator;
import cn.fay.chapter17.CollectionData;
/**
 * 自动生成数组，并可以填充数组内容
 * @author emellend
 *
 */
public class Generated {
	public static <T> T[] array(T[] a,Generator<T> gen) {
		return new CollectionData<T>(gen,a.length).toArray(a);
	}
	@SuppressWarnings("unchecked")
	public static <T> T[] array(Class<T> type,Generator<T> gen,int size) {
		T[] a = (T[]) Array.newInstance(type, size);
		return new CollectionData<T>(gen,size).toArray(a);
	}
	public static void main(String[] args) {
		Integer[] a={9,8,7,6};
		System.out.println(Arrays.toString(a));
		a=Generated.array(a, new CountingGenerator.Integer());
		System.out.println(Arrays.toString(a));
		Integer[] b=Generated.array(Integer.class, new CountingGenerator.Integer(), 15);
		System.out.println(Arrays.toString(b));
		
		System.out.println("==========================================");
		
		int[] ints = ConverTo.primitive(b);
		System.out.println(Arrays.toString(ints));
		
		boolean[] bs = ConverTo.primitive(Generated.array(Boolean.class,new  RandomGenerator.Boolean(), 17));
		System.out.println(Arrays.toString(bs));
		String[] strings = Generated.array(String.class, new RandomGenerator.String(10), 19);
		System.out.println(Arrays.toString(strings));
	}
}
